package sdk.webview.fmc.com.fmcsdk.activity;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import sdk.webview.fmc.com.fmcsdk.R;
import sdk.webview.fmc.com.fmcsdk.base.BaseActivity;
import sdk.webview.fmc.com.fmcsdk.base.BasePresenter;
import sdk.webview.fmc.com.fmcsdk.base.MyFragmentPagerAdapter;
import sdk.webview.fmc.com.fmcsdk.fragment.HuaweiFragment;
import sdk.webview.fmc.com.fmcsdk.fragment.XiaomiFragment;
import sdk.webview.fmc.com.fmcsdk.util.TitleBar;
import sdk.webview.fmc.com.fmcsdk.view.FrameStyleColorButton;

/**
 * 消息推送提示向导界面
 */
public class NotifySettingActivity extends BaseActivity implements View.OnClickListener {

    TitleBar titlebar;
    TextView tvHuawei;
    TextView tvXiaomi;
    LinearLayout llCategory;
    ViewPager vPager;
    View view1;
    View view2;
    FrameStyleColorButton tvSetting;
    /**
     * 分类界面用viewpager加载fragment
     */

    private ArrayList<Fragment> fragmentList;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_notify_setting;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        titlebar   = (TitleBar) findViewById(R.id.titlebar);
        tvHuawei = (TextView) findViewById(R.id.tv_huawei);
        tvXiaomi = (TextView) findViewById(R.id.tv_xiaomi);
        llCategory  = (LinearLayout) findViewById(R.id.ll_category);
        vPager = (ViewPager) findViewById(R.id.vpager);
        view1 = findViewById(R.id.view_1);
        view2 = findViewById(R.id.view_2);
        tvSetting   = (FrameStyleColorButton) findViewById(R.id.tv_setting);
        titlebar.setTitle(getString(R.string.message_setting));
        llCategory.getChildAt(0).setSelected(true);
        view1.setVisibility(View.VISIBLE);

        tvHuawei.setOnClickListener(this);
        tvXiaomi.setOnClickListener(this);
        tvSetting.setOnClickListener(this);

        //ViewPager初始化
        initViewPager();
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    private void initViewPager() {

        fragmentList = new ArrayList<Fragment>();
        Fragment hwfragment = new HuaweiFragment();//华为设置

        Fragment xmfragment = new XiaomiFragment();//小米设置


        fragmentList.add(hwfragment);
        fragmentList.add(xmfragment);

        //设置viewPager适配器
        vPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList));
        vPager.setCurrentItem(0);//设置当前显示标签页为第一页
        vPager.setOnPageChangeListener(new MyOnPageChangeListener());//页面变化时的监听器
    }

    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

        @Override
        public void onPageSelected(int arg0) {
            for (int i = 0; i < fragmentList.size(); i++) {
                llCategory.getChildAt(i).setSelected(i == arg0);
            }
            if (arg0 == 0) {
                view1.setVisibility(View.VISIBLE);
                view2.setVisibility(View.GONE);
            }else {
                view2.setVisibility(View.VISIBLE);
                view1.setVisibility(View.GONE);
            }
        }


    }

    @Override
    public void onClick(View v) {
     if (v.getId()==R.id.tv_setting){
         Intent intent = new Intent(Settings.ACTION_SETTINGS);
         context.startActivity(intent);
     }else if (v.getId()==R.id.tv_huawei){
         vPager.setCurrentItem(0);
         view1.setVisibility(View.VISIBLE);
         view2.setVisibility(View.GONE);
     }else if (v.getId()==R.id.tv_xiaomi){
         vPager.setCurrentItem(1);
         view2.setVisibility(View.VISIBLE);
         view1.setVisibility(View.GONE);
     }
    }

}